package com.sinoglobal.ningxia.thread;

import com.sinoglobal.ningxia.R;

/**
 * @author qiwx
 * @createdate 2014-7-24 下午7:05:02
 * @Description: 可重复使用的线程
 */
public  class ReusableThread extends Thread {
	private static ReusableThread reusableThread;
	private boolean isRun = true;// 是否结束线程的标志位
	private byte[] data;// 需要发送的字节流

	// 构造方法私有化
	private ReusableThread() {
	}

	// 获得对象
	public static ReusableThread getInstance() {
		if (reusableThread == null) {
			reusableThread = new ReusableThread();
			reusableThread.start();
			System.out.println("新建了一个发送线程");
		} else {
			System.out.println("使用已有的发送线程");
		}
		return reusableThread;
	}

	@Override
	public void run() {
		try {
			synchronized (this) {
				while (isRun) {
					System.out.println("线程开始运行");
					wait();
					System.out.println("线程被唤醒");
					System.out.println("发送的数据-->" + data[0] + data[1]);
				}
			}
			System.out.println("线程结束");
			reusableThread = null;
		} catch (InterruptedException e) {
			reusableThread = null;
			e.printStackTrace();
		}
	}
	// 唤醒线程
	public synchronized void setNotify() {
		notify();
	}

	public boolean isRun() {
		return isRun;
	}

	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}
}
